﻿/*
==================================================================
    注意：此文件为Auto代码，不可手动修改【监管部门】
==================================================================
*/

namespace Services
{
    /// <summary>
    /// Auto监管部门
    /// </summary>
    public abstract class AutoRegulatorMapping : BaseMapping<Regulator, RegulatorModel, RegulatorFilterModel>
    {
        public AutoRegulatorMapping(ICurrentUser currentUser)
            : base(currentUser) { }

        #region 读数据
        /// <summary>
        /// 读数据，从Entity对象转成Model对象
        /// </summary>
        /// <param name="entity">Entity对象</param>
        /// <returns></returns>
        public override RegulatorModel Entity2Model(Regulator entity)
        {
            RegulatorModel model = new RegulatorModel();

            model.RegulatorID = entity.RegulatorID;
            model.Code = entity.Code;
            model.Name = entity.Name;
            model.LeaderName = entity.LeaderName;
            model.LeaderPhone = entity.LeaderPhone;
            model.Remark = entity.Remark;
            model.IsDelete = entity.IsDelete;
            model.CreateDate = entity.CreateDate;

            return model;
        }
        #endregion

        #region 写数据
        /// <summary>
        /// 写数据，从Model对象转换成Entity对象
        /// </summary>
        /// <param name="model">ViewModel对象</param>
        /// <returns></returns>
        public override Regulator Model2Entity(RegulatorModel model)
        {
            Regulator entity = new Regulator();

            entity.RegulatorID = model.RegulatorID;
            entity.Code = model.Code;
            entity.Name = model.Name;
            entity.LeaderName = model.LeaderName;
            entity.LeaderPhone = model.LeaderPhone;
            entity.Remark = model.Remark;
            entity.IsDelete = model.IsDelete;
            entity.CreateDate = model.CreateDate;

            return entity;
        }
        #endregion

        #region 搜索条件转换
        /// <summary>
        /// 搜索条件转换，从FilterModel对象转换成Where对象
        /// </summary>
        /// <param name="filterModel">FilterModel对象</param>
        /// <returns></returns>
        public override Expressionable<Regulator> FilterModel2Where(RegulatorFilterModel filterModel)
        {
            var where = base.FilterModel2Where(filterModel);

            //编号
            if (filterModel._RegulatorID.HasValue)
            {
                where = where.And(x => x.RegulatorID == filterModel._RegulatorID.Value);
            }
            if (filterModel._RegulatorIDs.IsNotEmpty() && filterModel._RegulatorIDs.Count > 0)
            {
                where = where.And(x => filterModel._RegulatorIDs.Contains(x.RegulatorID));
            }
            if (filterModel._NotRegulatorIDs.IsNotEmpty() && filterModel._NotRegulatorIDs.Count > 0)
            {
                where = where.And(x => !filterModel._NotRegulatorIDs.Contains(x.RegulatorID));
            }

            //编码
            if (!string.IsNullOrEmpty(filterModel._Code))
            {
                where = where.And(x => x.Code.Contains(filterModel._Code));
            }
            if (!string.IsNullOrEmpty(filterModel._CodeEqual))
            {
                where = where.And(x => x.Code == filterModel._CodeEqual);
            }

            //名称
            if (!string.IsNullOrEmpty(filterModel._Name))
            {
                where = where.And(x => x.Name.Contains(filterModel._Name));
            }
            if (!string.IsNullOrEmpty(filterModel._NameEqual))
            {
                where = where.And(x => x.Name == filterModel._NameEqual);
            }

            //负责人
            if (!string.IsNullOrEmpty(filterModel._LeaderName))
            {
                where = where.And(x => x.LeaderName.Contains(filterModel._LeaderName));
            }
            if (!string.IsNullOrEmpty(filterModel._LeaderNameEqual))
            {
                where = where.And(x => x.LeaderName == filterModel._LeaderNameEqual);
            }

            //是否删除
            if (filterModel._IsDelete.HasValue)
            {
                where = where.And(x => x.IsDelete == filterModel._IsDelete.Value);
            }
            if (filterModel._IsDeletes.IsNotEmpty() && filterModel._IsDeletes.Count > 0)
            {
                where = where.And(x => filterModel._IsDeletes.Contains(x.IsDelete));
            }
            
            //创建时间
            if (filterModel._CreateDate.HasValue)
            {
                where = where.And(x => x.CreateDate == filterModel._CreateDate.Value);
            }
            if (filterModel._CreateDate_Begin.HasValue)
            {
                where = where.And(x => x.CreateDate >= filterModel._CreateDate_Begin.Value);
            }
            if (filterModel._CreateDate_End.HasValue)
            {
                where = where.And(x => x.CreateDate < filterModel._CreateDate_End.Value.AddDays(1));
            }

            return where;
        }
        #endregion
    }
}

